﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using GeoSketch2.Core;

namespace GeoSketch2.Archaeo
{
    /// <summary>
    /// The viewport for the Archaeomodule, include a digging height handling
    /// </summary>
    [Serializable]
    public class Viewport : BaseViewport
    {
        #region Fields
        ExcavationArea _CurrentExcavationArea;

        /// <summary>
        /// True if the digging fuction is activated
        /// </summary>
        public bool DiggingMode;

        public ArchaeoProject Project;
        #endregion

        #region Constructor

        /// <summary>
        /// The viewport for the Archaeomodule, include a digging height handling
        /// </summary>
        /// <param name="DisplayBox">The control to draw to</param>
        /// <param name="ExcavationSite">The current excavation site</param>
        public Viewport(DisplayBox DisplayBox, ArchaeoProject Project)
            : base(DisplayBox, Tools.GetRectangleDBounds(Project.MainSite.Bounds,YAxisDirection.UpSide))
        {
            this.Project = Project;
            DisplayControl = DisplayBox;
            CurrentExcavationArea = Project.MainSite;
            SortLayers();
            CurrentLayer = Layers[0];
        } 
        #endregion

        #region Methods
        /// <summary>
        /// The method to clear the display
        /// </summary>
        public override void ClearScene()
        {
            Region r = Graphic.Clip;
            Region nr = new Region();
            Graphic.Clip = nr;
            Graphic.Clear(Vars.Setup.StandardObjects.Viewport.ComplementRegionColor);
            Graphic.Clip = r;
            Graphic.Clear(Vars.Setup.StandardObjects.Viewport.BackgroundColor);
        } 
        #endregion

        #region Properties
        /// <summary>
        /// Gets the display bounds, the excavation area bounds, in screen coordinates
        /// </summary>
        protected override Region DisplayBounds
        {
            get
            {
                if (CurrentExcavationArea != null)
                {
                    PointF[] bounds_screen_coordinates = Object2ImageTransformation.TransformF(CurrentExcavationArea.Bounds);
                    return new Region(new GraphicsPath(bounds_screen_coordinates, Tools.GetPathPointsType(bounds_screen_coordinates.Length)));
                }
                else
                {
                    return base.DisplayBounds;
                }
            }
        }

        /// <summary>
        /// All objects of the site
        /// </summary>
        public override List<BaseDrawableObject> GeoObjects
        {
            get
            {
                return Project.ArchaeoObjects;
            }
            set
            {
                Project.ArchaeoObjects = value;
            }
        }

        /// <summary>
        /// All layers of the project
        /// </summary>
        public override List<Layer> Layers
        {
            get
            {
                return Project.ProjectLayers;
            }
            set
            {
                Project.ProjectLayers = value;
            }
        }

        /// <summary>
        /// Gets the linetype library of the project
        /// </summary>
        public override List<LineType> LineTypeLibrary
        {
            get
            {
                return Project.LineTypeLibrary;
            }
            set
            {
                Project.LineTypeLibrary = value;
            }
        }

        /// <summary>
        /// The current digging height
        /// </summary>
        public DiggingHeight CurrentDiggingHeight
        {
            get
            {
                return CurrentExcavationArea.DiggingHeight;
            }
        }

        /// <summary>
        /// Gets or sets the current excavation area
        /// </summary>
        public ExcavationArea CurrentExcavationArea
        {
            get
            {
                return _CurrentExcavationArea;
            }
            set
            {
                _CurrentExcavationArea = value;
                CurrentObjectView = value.Bounds.GetRectangleDBounds(YAxisDirection.UpSide);
                SetupGraphics();
            }
        } 
        #endregion
    }
}
